﻿/*
 * @Autor: PK
 * @Date: 2021/12/25 11:11:SS
 */

using GeneticAlgorithm.Components.Interfaces;
using GeneticAlgorithm.Interfaces;
using ShkSimulation.core.optimization.impl;
using ShkSimulation.core.optimization.interfaces;

namespace ShkSimulation.core.optimization.algorithm.classicGeneticAlgotithm {
	
	public class BasicEvaluator : IEvaluator {

		private GeneticAlgorithmVariableAdapter adapter;

		public BasicEvaluator(Optimization optimization) {
			this.Optimization = optimization;
			this.adapter = new GeneticAlgorithmVariableAdapter(optimization.VariableVectorTemplate);
		}

		Optimization Optimization { get; set; }

		public double Evaluate(IChromosome chromosome) {
			EvaluateResult result = Optimization
				.RunSingleThenEvaluate(adapter.Convert(chromosome));
			Optimization.BestOne = Optimization.BestOne ?? result;
			Optimization.BestOne = result > Optimization.BestOne ? result : Optimization.BestOne;
			return result.Valuation;
		}

	}

}